#====  PACKAGE =================================================================
#         NAME:  Parser
#      PURPOSE:  Contains several different ways for importing and exporting.
#   PARAMETERS:  none
#      RETURNS:  true
#===============================================================================

#- Overview --------------------------------------------------------------------
# import		export		
#			-export_gephi
#-------------------------------------------------------------------------------
package insj::Parser;
use strict;
use warnings;
use Carp;

#===  FUNCTION  ================================================================
#         NAME:  import
#      PURPOSE:  import choses the right subroutine for import
#   PARAMETERS:  format (String)
#      RETURNS:  true
#===============================================================================
sub import {
	}

#===  FUNCTION  ================================================================
#         NAME:  export
#      PURPOSE:  export choses the right subroutine for export
#   PARAMETERS:  path + name (String), format (String)
#      RETURNS:  true
#===============================================================================
sub export {

	my $path     = $_[0];
	my $format   = $_[1];
	my $self     = $_[2];

	if ( $format eq "gephi" or $format eq "Gephi" ) { _export_gephi( $path, $self) }	

	1;
	}

#===  FUNCTION  ================================================================
#         NAME:  _export_gephi
#      PURPOSE:  Exports to gephi.
#   PARAMETERS:  $path (String), $self (Network Object)
#      RETURNS:  true
#===============================================================================
sub _export_gephi {
	my $path     = $_[0];
	my $self     = $_[1];

	open (my $file, ">$path");

	$self->rewind;

	my $node = $self->get_next;
	
	my %hash = $node->get_state_hash;
		
	$self->rewind;

	# Gephi XML header
	print $file <<PLAINXML;
<gexf xmlns="http://www.gexf.net/1.1draft" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.gexf.net/1.1draft/gexf.xsd"
version="1.1">
<graph mode="dynamic" defaultedgetype="directed">
<attributes class="node" mode="dynamic">
PLAINXML

	while ( (my $key, my $value) = each %hash ){
  		if ($key !~ "_t" ){
			print $file "\t<attribute id=\"$key\" title=\"$key\" type=\"integer\"/>\n";
			}
		}

	print $file "</attributes>\n\t<nodes>\n";

	while( my $node = $self -> get_next ) {
		print $file "\n\t<node id=\"".$node->get_name."\" label=\"".$node->get_name."\">\n";
		my @array = $node->get_timeline;
		print $file "\t<attvalues>\n";
		while(<@array>){
			my @line = split(";",$_);
			print $file "\t<attvalue for=\"$line[0]\" value=\"$line[1]\" start=\"$line[2]\"/>\n" if defined $line[0];
			}
		print $file "\t</attvalues>\n\t</node>\n";
		}

	print $file "\n\t</nodes>\n\n";
	print $file "\t<edges>\n";

	$self -> rewind;

	while(my $node = $self -> get_next ) {
		$node -> rewind;
		while(my $edge = $node -> get_next ) {
			print $file "\t<edge source=\"".$edge->get_name."\" target=\"".$node->get_name."\" />\n";		
			}
		}
	print $file "\t</edges>\n</graph>\n</gexf>\n";
	1;
	}

1;
